package main

import (
	"fmt"
	"log"

	"gitee.com/Flearning/go_crawl/mgobson"
	"gitee.com/Flearning/go_crawl/xueqiu/userinfo"
)

var mgoXueqiuCollection string = "crawl_xueqiu_user"

func main() {
	// 初始化mongodb
	mgobson.NewInit()

	uc := userinfo.CrawlInfo{
		Page: "1",
	}

	var (
		statusCode, maxPage, nextPage int
		userIds, uids                 []string
		i, j, sigma                   int
	)

	userinfo.MgoWSet = true

	// allSigma, _ := sigmaAll()
	j = 0
	for j <= 20 {
		sigma = len(uids)
		if sigma > 0 {
			for _, v := range uids {
				uc.UserId = v
				i = 1
				for {
					statusCode, maxPage, nextPage, userIds = userinfo.UserCrawl(fmt.Sprintf("%d", i), uc.UserId)
					fmt.Println("this crawl `statusCode`: ", statusCode, "`MaxPage`: ", maxPage, "`NextPage`:", nextPage, "`CurrentUid`: ", uc.UserId)
					fmt.Println("this crawl data:", len(userIds))
					uc.Page = fmt.Sprintf("%d", nextPage)
					i++
					if i > maxPage {
						break
					}
				}
			}
		} else {
			skip := j + 20
			us := userSkipInfo(skip)
			uc.UserId = fmt.Sprintf("%d", us.Id)
			i = 1
			for {
				statusCode, maxPage, nextPage, userIds = userinfo.UserCrawl(fmt.Sprintf("%d", i), uc.UserId)
				fmt.Println("this crawl `statusCode`: ", statusCode, "`MaxPage`: ", maxPage, "`NextPage`:", nextPage, "`CurrentUid`: ", uc.UserId)
				fmt.Println("this crawl data:", len(userIds))
				uids = append(uids, userIds...)
				sigma = len(uids)
				fmt.Println("sigma:", sigma)
				i++
				if i > maxPage {
					break
				}
			}
		}
		log.Println("this for id:", j)
		j++
	}
}

func sigmaAll() (sigma int, err error) {
	c := mgobson.MgoSession.DB(mgobson.AuthDatabase).C(mgoXueqiuCollection)
	sigma, err = c.Find(nil).Count()
	return
}

func userSkipInfo(skip int) *userinfo.UserStruct {
	c := mgobson.MgoSession.DB(mgobson.AuthDatabase).C(mgoXueqiuCollection)
	us := new(userinfo.UserStruct)
	c.Find(nil).Sort("add_time").Skip(skip).Limit(1).One(us)
	return us
}
